<?php

namespace App\Agent\Controllers;

use App\Http\Controllers\BaseController;
use App\Models\AgentUser;
use App\Models\CashOrder;
use App\Models\GoodOrder;
use App\Models\GoodOrderDetail;
use App\Models\PaymentOrder;
use App\Models\ServiceOrder;
use App\Models\ShopUser;
use App\Models\User;
use App\Models\Worker;
use App\Shop\Forms\Cash\HandleCashOrder;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Widgets\Card;
use Dcat\Admin\Widgets\Modal;
use Dcat\Admin\Widgets\Tab;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;

class BasicController extends BaseController
{
    public function index(Content $content)
    {
        return $content
            ->translation($this->translation())
            ->title("数据看板")
            ->body(function (Row $row){
                $agentUser = auth("agent")->user();
                $agent_id = $agentUser->id;
                $data['agent'] = $agentUser;
                $type = $agentUser['type'];
                $data['cash_money'] = CashOrder::query()->where("agent_id",$agent_id)->where("type",2)
                    ->where("status",0)->sum("amount");
                $data['agent']['money'] = $data['agent']['money'] - $data['cash_money'];
                $data['agent']['used_money'] = floatval($data['agent']['used_money']);
                $data['agent']['shop_count'] = ShopUser::query()->where("agent_id",$agent_id)->count();
                if ($type){
                    $district_ids = explode(",",$agentUser['district_ids']);
                    $orderPrice = ServiceOrder::query()->whereIn("district_id",$district_ids)->where("status","<>",4)->sum("amount");
                    $orderCount = ServiceOrder::query()->whereIn("district_id",$district_ids)->where("status","<>",4)->count();
                }else{
                    $city_ids = explode(",",$agentUser['city_ids']);
                    $orderPrice = ServiceOrder::query()->whereIn("city_id",$city_ids)->where("status","<>",4)->sum("amount");
                    $orderCount = ServiceOrder::query()->whereIn("city_id",$city_ids)->where("status","<>",4)->count();
                }
                $data['orderPrice'] = $orderPrice;
                $data['orderCount'] = $orderCount;
                $tab = Tab::make();
                $data['modal'] = Modal::make()->lg()
                    ->title('提现申请')
                    ->body(HandleCashOrder::make())->id("modal-cash");
                $tab->add('基础数据', view('agent.basic.index',$data), true);
                $row->column(12,$tab->withCard());
            });
    }

    protected function getWhereRange($field = "created_at"): \Closure
    {
        return function ($query) use ($field) {
            $type = request()->post('type');
            $range = request()->post('range');
            if ($type=='custom'){
                $query->when($range,function ($q,$value){
                    $value[0] = Carbon::parse($value[0]/1000)->format("Y-m-d 00:00:00");
                    $value[1] = Carbon::parse($value[1]/1000)->format("Y-m-d 23:59:59");
                    $q->whereBetween("created_at",$value);
                });
            }else{
                $carbon = now();
                switch ($type){
                    case "yesterday":
                        $d = $carbon->addDays(-1);
                        $range[0] = $d->startOfDay()->format("Y-m-d 00:00:00");
                        $range[1] = $d->endOfDay()->format("Y-m-d 23:59:59");
                        break;
                    case "today":
                        $range[0] = $carbon->startOfDay()->format("Y-m-d 00:00:00");
                        $range[1] = $carbon->endOfDay()->format("Y-m-d 23:59:59");
                        break;
                    case "week":
                        $range[0] = $carbon->startOfWeek()->format("Y-m-d 00:00:00");
                        $range[1] = $carbon->endOfWeek()->format("Y-m-d 23:59:59");
                        break;
                    case "month":
                        $range[0] = $carbon->startOfMonth()->format("Y-m-d 00:00:00");
                        $range[1] = $carbon->endOfMonth()->format("Y-m-d 23:59:59");
                        break;
                    case "quarter":
                        $range[0] = $carbon->startOfQuarter()->format("Y-m-d 00:00:00");
                        $range[1] = $carbon->endOfQuarter()->format("Y-m-d 23:59:59");
                        break;
                    case "year":
                        $range[0] = $carbon->startOfYear()->format("Y-m-d 00:00:00");
                        $range[1] = $carbon->endOfYear()->format("Y-m-d 23:59:59");
                        break;
                    default:
                        $range = [];
                        break;
                }
                if ($range){
                    $query->whereBetween($field,$range);
                }
            }
        };
    }

    protected function range()
    {
        $whereRange = $this->getWhereRange();
        $agentUser = auth("agent")->user();
        $list = ShopUser::query()->select(['id','name'])
            ->withCount(['orders'=>function($query) use($whereRange) {
                $query->where($whereRange)->where("status","<>",4);
            },'orders as orders_sum'=>function($query) use($whereRange){
                $query->select(DB::raw("sum(amount)"))->where($whereRange)->where("status","<>",4);
            }])
            ->where("agent_id",$agentUser->id)
            ->orderByDesc("orders_count")->get();
        foreach ($list as $item){
            $item->orders_count = $item->orders_count?:0;
            $item->orders_sum = $item->orders_sum?:0;
        }
        $data['items'] = $list;
        return $this->response()->data($data);
    }


    public function cash(){
//        $shopUser = auth("shop")->user();
//        CashOrder::cash(2,$shopUser);
    }
}
